package com.laundry.controller;

import com.laundry.dto.ResultDTO;
import com.laundry.entity.Announcement;
import com.laundry.service.AnnouncementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/announcement")
public class AnnouncementController {
    
    @Autowired
    private AnnouncementService announcementService;
    
    @GetMapping("/list")
    public ResultDTO getAnnouncements() {
        try {
            return ResultDTO.success(announcementService.findAll());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDTO.error("获取公告列表失败");
        }
    }
    
    @GetMapping("/detail")
    public ResultDTO getAnnouncementById(@RequestParam Long id) {
        try {
            return ResultDTO.success(announcementService.findById(id));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDTO.error("获取公告详情失败");
        }
    }
    
    @PostMapping("/create")
    public ResultDTO createAnnouncement(@RequestBody Announcement announcement) {
        try {
            return ResultDTO.success(announcementService.save(announcement));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDTO.error("创建公告失败");
        }
    }
    
    @PutMapping("/update")
    public ResultDTO updateAnnouncement(@RequestBody Announcement announcement) {
        try {
            return ResultDTO.success(announcementService.update(announcement));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDTO.error("更新公告失败");
        }
    }
    
    @DeleteMapping("/delete")
    public ResultDTO deleteAnnouncement(@RequestParam Long id) {
        try {
            announcementService.deleteById(id);
            return ResultDTO.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDTO.error("删除公告失败");
        }
    }
} 